//<script language=javascript>


/*
	arrRuleList is printed on the page itself
*/

function Rule_OnCheck(ruleid, isChecked)
{	
	var f = document.frmMain;
	var ddEnforcement = document.getElementById("EnfLevel_" + ruleid);
	ddEnforcement.disabled = !isChecked;

	if (typeof(Rule_OnCheck_Override)!='undefined') Rule_OnCheck_Override(ruleid, isChecked);
	
	var formfields = arrRuleList[ruleid];
	if (formfields) {
		for (var i = 0; i < formfields.length; i++) {
			var fField = document.getElementById(formfields[i]);
			if (fField) {
				fField.style.backgroundColor = '';
				fField.disabled = !isChecked;
				switch (fField.type) {
					case 'text':
						//fField.value = '';
						break;
					case 'select-one':
						//fField.selectedIndex = 0;	
						break;
				}
			}
		}
	}
}

function Rule_Validation()
{	
	var f = document.frmMain;
	
	for (ruleid in arrRuleList) //ac ok
	{
		if (!otHasOwnProperty(arrRuleList, ruleid))
			continue;
	
		var chkRule = document.getElementById('rule_' + ruleid);
		if (chkRule && chkRule.checked)
		{
			var formfields = arrRuleList[ruleid];
			for (var i=0; i < formfields.length; i++)
			{
				var fField = document.getElementById(formfields[i])
				if (fField)
				{
					fField.style.backgroundColor = '';
				
					switch (fField.type)
					{
						case 'text':
							//sample of the layout of arrFieldArray
							//arrFieldArray['9_AirCostNonStop_difference_new'] = new Array('difference','money','0','99999999', true, 'validateMyField');							
							if (arrFieldArray[fField.name][4] && isBlank(fField.value))
							{
								var msg = OTGetUserMessage("ValidationsJS","FieldEmpty")
								setFieldError(fField, msg);
								return false;
							}
							var datatype = arrFieldArray[fField.name][1];
							var min = arrFieldArray[fField.name][2];
							var max = arrFieldArray[fField.name][3];
							
							switch (datatype)
							{
								case 'string':
									if (fField.value.length < min || fField.value.length > max)
									{
										var msg = OTFormatUserMessage("ValidationsJS", "FieldStringLength", min, max);
										setFieldError(fField, msg);
										return false;									
									}
									break;
								case 'date':
									if (!checkDate(fField, ""))
									{
										//errorMessage is a global far set by checkDate
										setFieldError(fField, errorMessage);
										return false;
									}
									break;
								case 'money':
									if (!validNumber(fField.value, min, max, true))
									{
										var msg = OTFormatUserMessage("ValidationsJS", "FieldNumberMinMax", min, max);
										setFieldError(fField, msg);
										return false;									
									}
									break;
								case 'int':
									if (!validNumber(fField.value, min, max, false))
									{
										var msg = OTFormatUserMessage("ValidationsJS", "FieldIntegerMinMax", min, max);
										setFieldError(fField, msg);
										return false;
									}
									break;
							}
							if(arrFieldArray[fField.name][5] != '')
							{
								if(!eval(arrFieldArray[fField.name][5] + '(fField)')) return false;
							}
							break;
						case 'select-one':
							break; //nothing to do.
						case 'select-multiple':
							break; //nothing to do.						
						case 'checkbox':
							break; //nothing to do.
						case 'radio':
							break; //nothing to do.
					}
				}
			}
		}
	}
	
	return true;
}

function setFieldError(field, msg)
{
	if (msg && msg.length > 0)
	{
		alert(msg);
	}

	field.onblur = function() {field.style.backgroundColor = '';};
	field.focus();
	field.select();	
	field.style.backgroundColor = 'orange';		
}

function enforceAllowOneClassUpgrade(ruleid)
{
	var formfields = arrRuleList[ruleid];
	for (var i=0; i < formfields.length; i++)
	{
		if (formfields[i].match("CabinClasses") != null)
		{
			var cabins = document.getElementById(formfields[i])
			var cabinSelectedIndex = cabins.selectedIndex;
			var enforceLevel = document.getElementById("EnfLevel_"+ruleid);
			var enforceAllowedIndex = 0;
			// Not very efficient but oh well
			var i = 0;
			// Find the index of allowed
			for (i=0;i<enforceLevel.options.length;i++)
			{
				if (enforceLevel.options[i].value=="0")
				{	
					enforceAllowedIndex = i;
				}
			}
			// Force allowed if one class upgrade is selected
			if (cabins.options[cabinSelectedIndex].value == "U" && enforceLevel.selectedIndex != enforceAllowedIndex) 
			{
				alert(OTFormatUserMessage("ValidationsJS", "OneClassUpgrageMustSelectAllowed"));
				enforceLevel.selectedIndex = enforceAllowedIndex;
			}
		}
	}

}
// ----------------------------- //
// Custom Validation Functions   //
// ----------------------------- //


/*	
	ac says don't bonk the user for entering the wrong case, just uppercase it all.
	Applies to AirportCodes Lists
*/
function listIsUpperCase(field) {
	
	//Ucase the whole string
	var s = String(field.value).toUpperCase();
	field.value = s;
	
	//If there are spaces then remove them
	if(s.search(/\s/)> 0){
		var index = s.search(/\s/);		
		for(i = 0; i < index; i++)	
		{
			field.value = String(field.value).replace(/\s/,"");
		}
	}	
	
	//If there are invalid chars then bonk the user with message.
	if(String(field.value).search(/[^\w\d,]/)> 0){
		var msg = OTFormatUserMessage("ValidationsJS", "InvalidCharactersInList");
		setFieldError(field,msg);
		return false;
	}
	
	
		return true;
}

// --------------------------------- //
// End Custom Validation Functions   //
// --------------------------------- //
